<?php

namespace Mouta\SeowebBundle\Menu;

use Knp\Menu\FactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Mopa\Bundle\BootstrapBundle\Navbar\AbstractNavbarMenuBuilder;
//use Mouta\SeowebBundle\Entity\Usuario;
//use Symfony\Component\Security\Core\SecurityContext;
//use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
//use Mouta\SeowebBundle\Controller\DefaultController;

class NavbarMenuBuilder extends AbstractNavbarMenuBuilder
{
    protected $securityContext;
    protected $isLoggedIn;

    public function __construct(FactoryInterface $factory, SecurityContextInterface $securityContext)
    {
        parent::__construct($factory);

        $this->securityContext = $securityContext;
        $this->isLoggedIn = $this->securityContext->isGranted('ROLE_USUARIO');
    }

    public function createMainMenu(Request $request)
    {
        $menu = $this->factory->createItem('root');
        $menu->setChildrenAttribute('class', 'nav');

        $menu->addChild('lala', array('route' => 'index'));

        return $menu;
    }

    public function createRightSideDropdownMenu(Request $request)
    {   
        $usuario = $this->securityContext->getToken()->getUser();
        $menu = $this->factory->createItem('root');
        $menu->setChildrenAttribute('class', 'nav pull-right');

        if ($this->isLoggedIn) {
            $dropdown = $this->createDropdownMenuItem($menu, "Administrar cuenta");
            $dropdown->addChild('Mi perfil', array('route' => 'editar_datos'));
            $dropdown->addChild('Mis sitios', array('route' => 'mis_sitios'));
            $dropdown = $this->createDropdownMenuItem($menu, "Usuario: ".$usuario->getEmail());
            $dropdown = $this->createDropdownMenuItem($menu, "Credito: ".$usuario->getCredito());
            $menu->addChild('Salir', array('route' => 'logout'));
        } else {
            $menu->addChild('Ingresar', array('route' => 'login'));
            $menu->addChild('Registrar', array('route' => 'registro'));
        }
        //$this->addDivider($menu, true);


        return $menu;
    }
}